package net.zjitc.service.Impl;

import net.zjitc.mapper.CategoryMapper;
import net.zjitc.mapper.OrderMapper;
import net.zjitc.mapper.ProductMapper;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Order;
import net.zjitc.pojo.OrderItem;
import net.zjitc.pojo.Products;
import net.zjitc.service.ProductService;
import net.zjitc.vo.ProductVo;
import net.zjitc.vo.Searchvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/4 8:13
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    OrderMapper orderMapper;
    @Override
    public List<Products> findAll() {
        return productMapper.findAll();
    }

    @Override
    public int add(Products products) {
        return productMapper.add(products);
    }

    @Override
    public void delete(String id) {
        productMapper.delete(id);
    }

    @Override
    public Products findById(String id) {
        return productMapper.findById(id);
    }

    @Override
    public List<Products> search(ProductVo productVo) {
        List<Products> list = productMapper.search(productVo);
        for (Products product :list){
            Category category = categoryMapper.findById(product.getCid());
            product.setCategory(category);
        }
        return list;
    }

    @Override
    public int update(Products products) {
        return productMapper.update(products);
    }

    @Override
    public Products findBy_pid(String id) {
        return productMapper.findBy_pid(id);
    }

    @Override
    public List<Products> finBycid(Integer id) {
        if (id == null)
        {
            return productMapper.findAll();
        }
        return productMapper.findBycid(id);
    }
    @Override
    public List<Products> SearchProduct(Searchvo vo) {
        return productMapper.SearchProduct(vo);
    }

    @Override
    public List<Products> findHot(String day) {
        List<Products> productList = new ArrayList<>();
        List<Order> ordersList = orderMapper.findByDayInInterval(day);
        //获取按销量排序的商品
        List<OrderItem> orderitemList = orderMapper.findItemByOrderid(ordersList);
        for (OrderItem item : orderitemList) {
            Products products = productMapper.findById(item.getPid());
            productList.add(products);
        }
        if (productList.size() > 0) {
            return productList;
        }
        return null;
    }
}
